import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { FindOrganizationUnitUsersInput, WorkflowServiceProxy, OrganizationUnitServiceProxy, UsersToOrganizationUnitInput, WorkflowStepViewDto, WorkflowSchemeContentInputDto } from '@shared/service-proxies/service-proxies';
import * as _ from 'lodash';
import { ModalDirective } from 'ngx-bootstrap';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { finalize } from 'rxjs/operators';

@Component({
  selector: 'ChoseUser',
  templateUrl: './ChoseUser.component.html',
  styleUrls: ['./ChoseUser.component.css']
})
export class ChoseUserComponent extends AppComponentBase {
  @ViewChild('modal') modal: ModalDirective;
  @ViewChild('dataTable') dataTable: Table;
  @ViewChild('paginator') paginator: Paginator;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  _workflowStepViewDto = new WorkflowStepViewDto;

  isShown = false;
  filterText = '';
  tenantId?: number;
  saving = false;
  selectedValues: string[] = [];
  constructor(injector: Injector,
    private _organizationUnitService: OrganizationUnitServiceProxy,
    private _workflowServiceProxy: WorkflowServiceProxy
  ) {
    super(injector);
  }
  show(workflowStepViewDto?: WorkflowStepViewDto): void {
    this._workflowStepViewDto = workflowStepViewDto;
    console.log(this._workflowStepViewDto)
    this.modal.show();
  }

  refreshTable(): void {
    this.paginator.changePage(this.paginator.getPage());
  }

  close(): void {
    this.modal.hide();
  }

  shown(): void {
    this.isShown = true;
    this.getRecordsIfNeeds(null);
  }

  getRecordsIfNeeds(event: LazyLoadEvent): void {
    if (!this.isShown) {
      return;
    }

    this.getRecords(event);
  }

  getRecords(event?: LazyLoadEvent): void {

    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);

      return;
    }

    this.primengTableHelper.showLoadingIndicator();

    const input = new FindOrganizationUnitUsersInput();
    input.filter = this.filterText;
    input.skipCount = this.primengTableHelper.getSkipCount(this.paginator, event);
    input.maxResultCount = this.primengTableHelper.getMaxResultCount(this.paginator, event);

    this._organizationUnitService
      .findUsersName(input)
      .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator()))
      .subscribe(result => {
        console.log(result.items)
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.primengTableHelper.records = result.items;
        this.primengTableHelper.hideLoadingIndicator();
      });
  }

  save(): void {
    const input = new WorkflowSchemeContentInputDto();
    console.log(this.selectedValues.toString())
    input.stepUser = this.selectedValues.toString();
    input.stepName = this._workflowStepViewDto.stepName;
    input.workflowName = this._workflowStepViewDto.workflowName;
    input.schemeVersion = this._workflowStepViewDto.schemeVersion;
    this.saving = true;

    this._workflowServiceProxy.insertOrUpdateWorkFolwSchemeContent(input)
    .pipe(finalize(() => { this.saving = false; }))
    .subscribe(() => {
        this.saving = false;
        this.selectedValues = [];
        this.notify.info(this.l('SavedSuccessfully'));
        this.close();
        this.modalSave.emit(null);
    });
   
  }
}
